

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=dark>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/newtubiao.png">
  <link rel="icon" href="/img/newtubiao.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="Asteri5m">
  <meta name="keywords" content="">
  <meta name="description" content="0x00 题记近一个月没有写wp了，今天正好有空，那就随手记一记 天天搞安全技术也是一件比较烦躁的事情，刚好碰上了网络编程课老师安排做项目，所以趁机换换口味，搞搞开发  知识点 win32开发，调用各种API 窗口类，窗体的属性 win32消息机制，消息循环 按钮的响应事件 Edit编辑框的内容获取 多字节到宽字节   EasyX图形库的使用 图片的处理 文字的输出 鼠标滞留效果的逻辑原理 虚拟按">
<meta property="og:type" content="article">
<meta property="og:title" content="记一次网络(局域网)程序项目开发(一)">
<meta property="og:url" content="http://asteri5m.icu/archives/%E8%AE%B0%E4%B8%80%E6%AC%A1%E7%BD%91%E7%BB%9C(%E5%B1%80%E5%9F%9F%E7%BD%91)%E7%A8%8B%E5%BA%8F%E9%A1%B9%E7%9B%AE%E5%BC%80%E5%8F%91.html">
<meta property="og:site_name" content="Asteri5m">
<meta property="og:description" content="0x00 题记近一个月没有写wp了，今天正好有空，那就随手记一记 天天搞安全技术也是一件比较烦躁的事情，刚好碰上了网络编程课老师安排做项目，所以趁机换换口味，搞搞开发  知识点 win32开发，调用各种API 窗口类，窗体的属性 win32消息机制，消息循环 按钮的响应事件 Edit编辑框的内容获取 多字节到宽字节   EasyX图形库的使用 图片的处理 文字的输出 鼠标滞留效果的逻辑原理 虚拟按">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/Asteri5m/wd_img/raw/master/img/image-20211021134951202.png">
<meta property="og:image" content="https://gitee.com/Asteri5m/wd_img/raw/master/img/image-20211022152820976.png">
<meta property="article:published_time" content="2021-10-23T04:55:27.456Z">
<meta property="article:modified_time" content="2021-11-22T14:55:37.448Z">
<meta property="article:author" content="Asteri5m">
<meta property="article:tag" content="Win32">
<meta property="article:tag" content="EasyX">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://gitee.com/Asteri5m/wd_img/raw/master/img/image-20211021134951202.png">
  
    <meta name="baidu-site-verification" content="code-GBSY8p4qe6" />
  
  <title>记一次网络(局域网)程序项目开发(一) - Asteri5m</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/atom-one-dark.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->

  
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/aplayer@1.10.0/dist/APlayer.min.css">
<link rel="stylesheet" href="/xm_custom/custom.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"asteri5m.icu","root":"/","version":"1.8.12","typing":{"enable":true,"typeSpeed":120,"cursorChar":"_","loop":true},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":3},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"5INqyf5xMrWdsn0whn39qjsu-gzGzoHsz","app_key":"6UTAOxyJnjvDwHX3PJagKMg9","server_url":"https://5inqyf5x.lc-cn-n1-shared.com","path":"window.location.pathname"}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Asteri5m</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/guestbook/">
                <i class="iconfont icon-note"></i>
                留言板
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" false
         style="background: url('/img/none.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="记一次网络(局域网)程序项目开发(一)">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-10-23 12:55" pubdate>
        2021年10月23日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      14k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      44 分钟
    </span>
  

  
  
    
      <!-- LeanCloud 统计文章PV -->
      <span id="leancloud-page-views-container" class="post-meta" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="leancloud-page-views"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">记一次网络(局域网)程序项目开发(一)</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：3 个月前
                
              </p>
            
            <div class="markdown-body">
              <h2 id="0x00-题记"><a href="#0x00-题记" class="headerlink" title="0x00 题记"></a>0x00 题记</h2><p>近一个月没有写wp了，今天正好有空，那就随手记一记</p>
<p>天天搞安全技术也是一件比较烦躁的事情，刚好碰上了网络编程课老师安排做项目，所以趁机换换口味，搞搞开发</p>
<hr>
<h2 id="知识点"><a href="#知识点" class="headerlink" title="知识点"></a>知识点</h2><ol>
<li>win32开发，调用各种API<ul>
<li>窗口类，窗体的属性</li>
<li>win32消息机制，消息循环</li>
<li>按钮的响应事件</li>
<li>Edit编辑框的内容获取</li>
<li>多字节到宽字节</li>
</ul>
</li>
<li>EasyX图形库的使用<ul>
<li>图片的处理</li>
<li>文字的输出</li>
<li>鼠标滞留效果的逻辑原理</li>
<li>虚拟按钮的实现及响应事件</li>
<li>多线程绘图</li>
</ul>
</li>
</ol>
<hr>
<p>以项目为例，解析说明上述知识点</p>
<hr>
<h2 id="0x01-Win32"><a href="#0x01-Win32" class="headerlink" title="0x01 Win32"></a>0x01 Win32</h2><p>如果说整个项目中什么最令我头疼，那绝对是win32的这一部分。可能使用现成的MFC组件会比较轻松，但是为了更好的理解这玩意儿，只能恶心自己了。</p>
<h3 id="窗口类、窗体"><a href="#窗口类、窗体" class="headerlink" title="窗口类、窗体"></a>窗口类、窗体</h3><p>在说之前先介绍一下窗口类</p>
<p>windows下窗口类分为三种：</p>
<ul>
<li><p>系统窗口类</p>
<p>​    由操作系统注册的。一些系统窗口类是所有进程都可以访问的，一些系统窗口类是只能被操作系统内部使用的。对于系统窗口类，应用程序是不能销毁的。</p>
</li>
<li><p>应用程序全局窗口类</p>
<p>​    指由可执行程序或者DLL注册的，可以被当前进程其他模块使用的窗口类。比如你在某个DLL中注册一个全局窗口类，应用程序可以通过加载该DLL之后就可以使用对应的窗口类。</p>
</li>
<li><p>应用程序局部窗口类</p>
<p>​    指由可执行程序或者DLL注册的，仅用于当前模块的窗口类。我们可以注册很多局部窗口类，但推荐的做法是仅注册一个窗口类，用于创建应用程序主窗口。</p>
</li>
</ul>
<p>微软其实是不建议大家做太多的全局窗口类的，具体原因原因在文中解释。</p>
<p>在VS下随手建一个win32项目(区别与控制台项目)，一切默认，不选择空项目，这时候就会看到一个已经制作好的窗口程序，有“精美”的窗口，较为完善的消息机制</p>
<p>而事实上，建一个空项目，用 <code>MessageBox()</code>函数就能得到一个最简单的窗体，虽然点了确认键他就关闭了，没什么别的功能。</p>
<p>所以这里用到另一个函数 <code>CreateWindow()</code> ，这个函数的原型是：</p>
<figure class="highlight c++"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs c++"><span class="hljs-function">HWND <span class="hljs-title">CreateWindow</span><span class="hljs-params">(</span></span><br><span class="hljs-params"><span class="hljs-function">	LPCTSTR lpClassName,<span class="hljs-comment">//指向一个空结束的字符串或者整型数atom</span></span></span><br><span class="hljs-params"><span class="hljs-function">	LPCTSTR lpWindowName,<span class="hljs-comment">//指向一个知道窗口名的空结束符 指针</span></span></span><br><span class="hljs-params"><span class="hljs-function">	DWORD dwStyle,<span class="hljs-comment">//指定常见窗口的风格</span></span></span><br><span class="hljs-params"><span class="hljs-function">	<span class="hljs-keyword">int</span> x,<span class="hljs-comment">//窗口的初始水平位置</span></span></span><br><span class="hljs-params"><span class="hljs-function">	<span class="hljs-keyword">int</span> y,<span class="hljs-comment">//窗口的初始垂直位置</span></span></span><br><span class="hljs-params"><span class="hljs-function">	<span class="hljs-keyword">int</span> nWidth，<span class="hljs-comment">//以设备单元指明窗口的宽度</span></span></span><br><span class="hljs-params"><span class="hljs-function">	<span class="hljs-keyword">int</span> nHeight，<span class="hljs-comment">//以设备单元指明窗口的宽度</span></span></span><br><span class="hljs-params"><span class="hljs-function">	HWND hWndParent,<span class="hljs-comment">//指向被创建窗口的父窗口或所有者窗口的句柄。</span></span></span><br><span class="hljs-params"><span class="hljs-function">	HMENU hMenu，<span class="hljs-comment">//菜单句柄,或依赖窗口风格指明一个子窗口标识</span></span></span><br><span class="hljs-params"><span class="hljs-function">	HANDLE hlnstance，<span class="hljs-comment">//与窗口相关联的模块实例的句柄。</span></span></span><br><span class="hljs-params"><span class="hljs-function">	LPVOID lpParam）；<span class="hljs-comment">//指向一个值的指针，该值传递给窗口WM_CREATE消息。</span></span></span><br><span class="hljs-params"><span class="hljs-function">    </span></span><br><span class="hljs-params"><span class="hljs-function"><span class="hljs-comment">//返回值：如果函数成功，返回值为新窗口的句柄：如果函数失败，返回值为NULL。若想获得更多错误信息，需调用GetLastError函数。</span></span></span><br></code></pre></div></td></tr></table></figure>

<p>像窗口风格，微软是已经给你封装好了的，你只需要查阅文档再根据需求选择就好，不需要从头编写，这点算是比较好的了。其他的参数，具体怎么用的，查查文档或者百度就都明白了。</p>
<p>注册窗口类，在创建窗口之前的操作。当然了，在他之前，还要设计窗口类先。</p>
<p>一览代码先，需要注意的是，CreateWindow函数的第一个参数，窗口类名：</p>
<figure class="highlight c++"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs c++"><span class="hljs-comment">//窗口处理函数</span><br><span class="hljs-function">LRESULT CALLBACK <span class="hljs-title">WindowProc</span><span class="hljs-params">(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)</span></span>;<br><br>TCHAR szAppClassName[] = <span class="hljs-built_in">TEXT</span>(<span class="hljs-string">&quot;FIRST&quot;</span>);<span class="hljs-comment">//自动适应字符集</span><br><br><span class="hljs-function"><span class="hljs-keyword">int</span> WINAPI <span class="hljs-title">WinMain</span><span class="hljs-params">(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, <span class="hljs-keyword">int</span> nCmdShow)</span></span><br><span class="hljs-function"></span>&#123;<br>	<span class="hljs-comment">//1.设计窗口类	Spy++	&quot;FIRST&quot;	</span><br>	WNDCLASS wndClass = &#123; <span class="hljs-number">0</span> &#125;;<br>	wndClass.hbrBackground = (HBRUSH)<span class="hljs-built_in">GetStockObject</span>(WHITE_BRUSH);	<span class="hljs-comment">//加载白色画刷</span><br>	wndClass.hCursor = <span class="hljs-built_in">LoadCursor</span>(<span class="hljs-literal">NULL</span>, IDC_ARROW);				<span class="hljs-comment">//加载光标</span><br>	wndClass.hInstance = hInstance;							   <span class="hljs-comment">//加载应用程序实例句柄</span><br>	wndClass.lpfnWndProc = WindowProc;						   <span class="hljs-comment">//窗口处理函数</span><br>	wndClass.lpszClassName = szAppClassName;				    <span class="hljs-comment">//窗口类型名</span><br>	wndClass.style = CS_HREDRAW | CS_VREDRAW;					<span class="hljs-comment">//窗口类的风格</span><br>	<br>	<span class="hljs-comment">//2.注册窗口类</span><br>	<span class="hljs-built_in">RegisterClass</span>(&amp;wndClass);<br><br>	<span class="hljs-comment">//3.创建窗口</span><br>	HWND hWnd = <span class="hljs-built_in">CreateWindow</span>(szAppClassName, <span class="hljs-built_in">TEXT</span>(<span class="hljs-string">&quot;欢迎&quot;</span>), WS_BORDER | WS_CAPTION | WS_SYSMENU |  WS_MINIMIZEBOX,<br>							<span class="hljs-number">570</span>, <span class="hljs-number">310</span>, <span class="hljs-number">400</span>, <span class="hljs-number">300</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, hInstance, <span class="hljs-literal">NULL</span>);<br>	g_hWnd = hWnd;<br>	<span class="hljs-comment">//4.显示和更新窗口</span><br>	<span class="hljs-built_in">ShowWindow</span>(hWnd, SW_SHOW);<br>	<span class="hljs-built_in">UpdateWindow</span>(hWnd);<br>	<span class="hljs-comment">//5.消息循环</span><br>	MSG msg;<br>	<span class="hljs-keyword">while</span> (<span class="hljs-built_in">GetMessage</span>(&amp;msg, <span class="hljs-literal">NULL</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>))<span class="hljs-comment">//WM_QUIT消息,返回0,结束循环</span><br>	&#123;<br>		<span class="hljs-comment">//将虚拟键消息转换为字符消息,虚拟键值</span><br>		<span class="hljs-built_in">TranslateMessage</span>(&amp;msg);<br>		<span class="hljs-comment">//将消息分发给窗口处理函数</span><br>		<span class="hljs-built_in">DispatchMessage</span>(&amp;msg);<br>	&#125;<br>	<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></div></td></tr></table></figure>

<p><code>WinMain</code>函数是窗口程序的入口点，这这个main里面，就是一个窗口的基本结构</p>
<pre><code class=" mermaid">graph LR
ks(开始) --&gt; sj[设计窗口类] 
sj --&gt; zc[注册窗口类] 
zc --&gt; cj[创建窗口]
cj --&gt; xs[显示窗口]
xs --&gt; xh((消息循环))
</code></pre>

<h3 id="消息机制"><a href="#消息机制" class="headerlink" title="消息机制"></a>消息机制</h3><p>在上述的消息循环中，就是他的消息机制，在设计窗口类时就已经说明了他的窗口处理函数 <code>wndClass.lpfnWndProc = WindowProc</code>。所以所有的消息处理都应该在该函数内完成，直到窗口关闭，退出循环，程序关闭。当然也可以使用其他阻塞函数使程序不关闭。</p>
<p>在这个消息机制中，我在它创建的时候（获取到创建消息），为它创建了许多子窗口，一些按钮和编辑框，用于人机交互。</p>
<p>在得到绘图消息的时候，我为该窗口进行了一系列的绘制，包括显示文字，设置背景(查看<a href="#jump00">详细代码</a>)<span id="return">等等</span>，给交互提供一定的提示。</p>
<figure class="highlight c++"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs c++"><span class="hljs-built_in"><span class="hljs-keyword">switch</span></span> (uMsg)<span class="hljs-comment">//获取消息</span><br>	&#123;<br>	<span class="hljs-keyword">case</span> WM_CREATE:<span class="hljs-comment">//窗口创建消息</span><br>		_ZWnd = <span class="hljs-built_in">CreateWindow</span>(<span class="hljs-string">L&quot;Edit&quot;</span>,<span class="hljs-literal">NULL</span>, WS_VISIBLE | WS_CHILD | WS_BORDER,<br>			<span class="hljs-number">135</span>, <span class="hljs-number">89</span>, <span class="hljs-number">150</span>, <span class="hljs-number">22</span>, hWnd, (HMENU)Edit_Z, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>);<br>		<span class="hljs-comment">/*...创建其他窗口...*/</span><br>		<span class="hljs-keyword">break</span>;<br>	<span class="hljs-keyword">case</span> WM_PAINT:<span class="hljs-comment">//窗口绘图消息</span><br>	&#123;<br>		<span class="hljs-comment">/* 绘图操作 */</span><br>	     <span class="hljs-built_in">TextOut</span>(hdc, <span class="hljs-number">95</span>, <span class="hljs-number">90</span>, <span class="hljs-string">L&quot;账户：&quot;</span>, <span class="hljs-number">3</span>);<br>	    <span class="hljs-comment">/* ...... */</span><br>		<span class="hljs-keyword">break</span>;<br>	&#125;<br>	<span class="hljs-keyword">case</span> WM_CLOSE:<span class="hljs-comment">//窗口关闭消息</span><br>		<span class="hljs-built_in">DestroyWindow</span>(hWnd);<span class="hljs-comment">//API函数</span><br>		<span class="hljs-keyword">break</span>;<br>	<span class="hljs-keyword">case</span> WM_DESTROY:<span class="hljs-comment">//窗口销毁消息</span><br>		<span class="hljs-built_in">PostQuitMessage</span>(<span class="hljs-number">0</span>);<span class="hljs-comment">//API函数，发送WM_QUIT消息,退出</span><br>	<span class="hljs-keyword">default</span>:<br>		<span class="hljs-keyword">break</span>;<br>	&#125;<br>	<span class="hljs-keyword">return</span> <span class="hljs-built_in">DefWindowProc</span>(hWnd, uMsg, wParam, lParam);<span class="hljs-comment">//返回值</span><br>&#125;<br></code></pre></div></td></tr></table></figure>

<p>最后我就得到了这样一个界面</p>
<p><img src="https://gitee.com/Asteri5m/wd_img/raw/master/img/image-20211021134951202.png" srcset="/img/loading.gif" lazyload></p>
<h3 id="按钮的响应事件"><a href="#按钮的响应事件" class="headerlink" title="按钮的响应事件"></a>按钮的响应事件</h3><p>按钮和编辑框都是窗口的一种，按钮是可以点击并且具有返回值的。在创建按钮的初期，是需要给按钮一个<em>菜单句柄</em><sup id="fnref:1" class="footnote-ref"><a href="#fn:1" rel="footnote"><span class="hint--top hint--rounded" aria-label="这里其实是依据窗口风格指明一个子窗口标识。对于层叠或弹出式窗口，hMenu指定窗口使用的菜单：如果使用了菜单类，则hMenu可以为NULL。对于子窗口，hMenu指定了该子窗口标识（一个整型量），一个对话框使用这个整型值将事件通知父类。应用程序确定子窗口标识，这个值对于相同父窗口的所有子窗口必须是唯一的。
">[1]</span></a></sup>，在获取了按钮按下的消息后，就会返回这个菜单句柄，从而确定是哪个按钮，进而进行下一步的响应。</p>
<p>在这里，我使用了一个不是我定义的窗口类的类名<code>“Button”</code>。</p>
<figure class="highlight c++"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs c++"><span class="hljs-built_in"><span class="hljs-keyword">switch</span></span> (uMsg)<span class="hljs-comment">//获取消息</span><br>	&#123;<br>	<span class="hljs-keyword">case</span> WM_CREATE:<span class="hljs-comment">//窗口创建消息</span><br>        <span class="hljs-built_in">CreateWindow</span>(<span class="hljs-string">L&quot;Button&quot;</span>, <span class="hljs-string">L&quot;注册&quot;</span>, WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,<br>			<span class="hljs-number">110</span>, <span class="hljs-number">215</span>, <span class="hljs-number">60</span>, <span class="hljs-number">20</span>, hWnd, (HMENU)Btn_Z, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>);<br>		<span class="hljs-built_in">CreateWindow</span>(<span class="hljs-string">L&quot;Button&quot;</span>, <span class="hljs-string">L&quot;登录&quot;</span>, WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,<br>				<span class="hljs-number">230</span>, <span class="hljs-number">215</span>, <span class="hljs-number">60</span>, <span class="hljs-number">20</span>, hWnd, (HMENU)Btn_D, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>);<br>		<span class="hljs-keyword">break</span>;<br>	<span class="hljs-keyword">case</span> WM_COMMAND:<span class="hljs-comment">//按键按下</span><br>	&#123;<br>		<span class="hljs-built_in"><span class="hljs-keyword">switch</span></span> (<span class="hljs-built_in">LOWORD</span>(wParam))<br>		&#123;<br>		<span class="hljs-keyword">case</span> Btn_D:<span class="hljs-comment">//点击登录；这里的值是我的宏定义</span><br>			<span class="hljs-comment">//获取输入框内容及其他登录相关操作</span><br>			<span class="hljs-keyword">break</span>;<br>		<span class="hljs-keyword">case</span> Btn_Z:<span class="hljs-comment">//点击注册</span><br>			<span class="hljs-comment">//进入注册界面</span><br>             <span class="hljs-keyword">break</span>;<br>		<span class="hljs-keyword">default</span>:<br>			<span class="hljs-keyword">break</span>;<br>		&#125;<br>        <span class="hljs-keyword">break</span>;<br>	&#125;<br>	<span class="hljs-keyword">default</span>:<br>		<span class="hljs-keyword">break</span>;<br>	&#125;<br>	<span class="hljs-keyword">return</span> <span class="hljs-built_in">DefWindowProc</span>(hWnd, uMsg, wParam, lParam);<br>&#125;<br></code></pre></div></td></tr></table></figure>

<p>可以直观的看到（事实上这里展示的时候我省略了很多代码，主要展示相关的内容），消息处理函数越来越庞大，越来越繁杂，因为这才是他的核心，他的大脑。</p>
<p>关于注册界面，因为这个函数越写越大，变得及其不方便处理，所以我就写了一个跳转函数，为注册界面也设计了窗口和消息机制，大体和这个差不多，这样做能够减少我的工程量，至于代码量……嘿嘿，DDDD<sup id="fnref:2" class="footnote-ref"><a href="#fn:2" rel="footnote"><span class="hint--top hint--rounded" aria-label="懂得都懂
">[2]</span></a></sup></p>
<h3 id="Edit内容获取"><a href="#Edit内容获取" class="headerlink" title="Edit内容获取"></a>Edit内容获取</h3><p>编辑框也是窗口的一种，不同于其他窗口，他可以输入，这才是交互的关键，因为如果什么都靠按钮来实现？那怕是要整一个虚拟键盘才能满足需求吧。</p>
<p>用下面的代码即可生成一个编辑框</p>
<figure class="highlight c++"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs c++"><span class="hljs-built_in">CreateWindow</span>(<span class="hljs-string">L&quot;Edit&quot;</span>,<span class="hljs-literal">NULL</span>, WS_VISIBLE | WS_CHILD | WS_BORDER,<span class="hljs-number">135</span>, <span class="hljs-number">89</span>, <span class="hljs-number">150</span>, <span class="hljs-number">22</span>, hWnd, (HMENU)Edit_Z, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>);<br></code></pre></div></td></tr></table></figure>

<p>这里已经是用到的第四个窗口类了（我自己定义的登录和注册页面是两个局部窗口类）。还有两个则是我没有定义就能直接使用的，则是系统窗口类，已经由微软打包好等待你调用就行。</p>
<p>我们自己定义的，通常是局部窗口类，当我们使用CreateWindow函数的时候，这个函数会拿着类名去局部串口类查找，找到相同的了，则返回，没找到接着查找全局窗口类，还没找到最后才是局部窗口类。如果我们把所有的窗口类都定义为全局窗口类，无疑会增加每次查找窗口类名的时间以及内存资源开销。甚至导致崩溃。</p>
<p>回到之前的问题：为什么微软不建议定义全局窗口类？应该是心里有答案了吧。</p>
<p>关于Edit窗口类的内容的获取，又是怎么实现的呢，这需要依靠<code>GetWindowText()</code>函数。该函数有三个参数：</p>
<figure class="highlight c++"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs c++"><span class="hljs-function">Int <span class="hljs-title">GetWindowText</span><span class="hljs-params">(HWND hWnd,<span class="hljs-comment">//带文本的窗口或控件的句柄。</span></span></span><br><span class="hljs-params"><span class="hljs-function">                  LPTSTR lpString,<span class="hljs-comment">//指向接收文本的缓冲区的指针。</span></span></span><br><span class="hljs-params"><span class="hljs-function">                  Int nMaxCount)</span></span>;<span class="hljs-comment">//指定要保存在缓冲区内的字符的最大个数，其中包含NULL字符。如果文本超过界限，它就被截断。</span><br></code></pre></div></td></tr></table></figure>

<p>  因此，通过此函数，再结合按钮响应事件，即可读取文本到<code>IpString</code>中，完成账号密码的交互功能。</p>
<h3 id="多字节到宽字节"><a href="#多字节到宽字节" class="headerlink" title="多字节到宽字节"></a>多字节到宽字节</h3><p>首先介绍什么是多字节。</p>
<p><strong>MBCS，Multi-Byte Character Set</strong>：指用多个字节来表示一个字符的字符编码集合。一般英文字母用1Byte，汉语等用2Byte来表示。兼容ASCII 127。</p>
<p>在最初的时候，Internet上只有一种字符集——ANSI的<strong>ASCII字符集</strong>，它使用7 bits来表示一个 字符，总共表示128个字符，其中包括了 英文字母、数字、标点符号等常用字符。</p>
<p>为了扩充<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/ASCII%E7%BC%96%E7%A0%81">ASCII编码</a>，以用于显示本国的语言，不同的国家和地区制定了不同的标准，由此产生了 GB2312, BIG5, JIS 等各自的编码标准。这些使用 2 个<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E5%AD%97%E8%8A%82">字节</a>来代表一个字符的各种汉字延伸编码方式，称为 <strong>ANSI 编码</strong>，又称为”MBCS（Multi-Bytes Character Set，多字节字符集）”。</p>
<p>不同 ANSI 编码之间互不兼容，当信息在国际间交流时，无法将属于两种语言的文字，存储在同一段 ANSI 编码的文本中。一个很大的缺点是，同一个编码值，在不同的编码体系里代表着不同的字。这样就容易造成混乱。</p>
<p><em>在这样混乱的时代，没有统一的编码是一件很麻烦的事，所以就有宽字节的诞生</em>。为了统一所有文字的编码，Unicode应运而生。Unicode把所有语言都统一到一套编码里，这样就不会再有乱码问题了。</p>
<p><strong>宽字节字符集：一般指Unicode编码的字符集</strong></p>
<p>Unicode称为统一码或万国码，统一了不同国家的字符编码。</p>
<p>Unicode通常<strong>用两个字节表示一个字符</strong>，原有的英文编码从单字节变成双字节，只需要把高字节全部填为0就可以。</p>
<p>但是宽字节也不是完美无缺，因为它的效率并不高，比如UCS-4(Unicode的标准之一)规定用4个字节存储一个符号，那么每个英文字母前都必然有三个字节是0，这对存储和传输来说都很耗资源。为了提高Unicode的编码效率，于是就出现了<strong>UTF-8</strong>编码。UTF-8可以根据不同的符号自动选择编码的长短。比如英文字母可以只用1个字节就够了。(这里就不深究了。)</p>
<p>刚刚讲了一大堆，但这由有什么意义呢？和我的程序和项目又有什么关系呢？在Edit窗口获取的文本，就是宽字节格式，但是事实上，在我程序中，我将所有的输入字符（账号和密码）都限制在了多字节字符集。因此我还沿用宽字节，无疑就会造成不必要的资源浪费。而且在我后面的Socket通信中，相关的API函数<code>send()</code>和<code>recv()</code>则是需要多字节才能传输，若是硬要传输宽字节，又得自己写个协议封装一下，何必呢。</p>
<p>了解了多字节和宽字节，互转则是目前需要做的首要工作</p>
<figure class="highlight c++"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs C++"><span class="hljs-comment">//TCHAR转换为char</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">TCHARtoChar</span><span class="hljs-params">(TCHAR* tbuffer, <span class="hljs-keyword">char</span>* buffer)</span></span><br><span class="hljs-function"></span>&#123;<br>	<span class="hljs-keyword">int</span> len = <span class="hljs-built_in">WideCharToMultiByte</span>(CP_ACP, <span class="hljs-number">0</span>, tbuffer, <span class="hljs-number">-1</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-number">0</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>);<br>	<span class="hljs-built_in">WideCharToMultiByte</span>(CP_ACP, <span class="hljs-literal">NULL</span>, tbuffer, <span class="hljs-number">-1</span>, buffer, len, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>);<br>	<span class="hljs-keyword">return</span>;<br>&#125;<br><br><span class="hljs-comment">//char转换为TCHAR</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">ChartoTCHAR</span><span class="hljs-params">(<span class="hljs-keyword">char</span>* buffer, TCHAR* tbuffer)</span></span><br><span class="hljs-function"></span>&#123;<br>	<span class="hljs-keyword">int</span> len = <span class="hljs-built_in">MultiByteToWideChar</span>(CP_ACP, <span class="hljs-number">0</span>, buffer, <span class="hljs-built_in">strlen</span>(buffer) + <span class="hljs-number">1</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-number">0</span>);<br>	<span class="hljs-built_in">MultiByteToWideChar</span>(CP_ACP, <span class="hljs-literal">NULL</span>, buffer, <span class="hljs-built_in">strlen</span>(buffer) + <span class="hljs-number">1</span>, tbuffer, len);<br>	<span class="hljs-keyword">return</span>;<br>&#125;<br></code></pre></div></td></tr></table></figure>

<hr>
<h2 id="0x02-EasyX"><a href="#0x02-EasyX" class="headerlink" title="0x02 EasyX"></a>0x02 EasyX</h2><p>为什么要用的EasyX？因为他是个图形库，能够极大的缩减图形化界面的工程量。另一方面则是这玩意我比较熟，相比于使用WinAPI绘图，我更喜欢这个相对简单的操作。</p>
<p>在使用WinAPI绘图时，我尝试给登录界面增加背景图片然后放弃了，就调了一个颜色作为背景，其次就是输出了几个字也是及其的麻烦。具体有多麻烦，看看代码吧(如果你是<span id="jump00">跳转</span>过来的，<a href="#return">点击这里返回</a>)</p>
<figure class="highlight c++"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs C++"><span class="hljs-keyword">case</span> WM_PAINT:<span class="hljs-comment">//窗口绘图消息</span><br>	&#123;<br>		PAINTSTRUCT ps;     <span class="hljs-comment">//首先要定义一个paint结构体</span><br>		HDC hdc = <span class="hljs-built_in">BeginPaint</span>(hWnd, &amp;ps);   <span class="hljs-comment">//初始化</span><br>		<span class="hljs-built_in">OnPaint</span>(hdc);		<span class="hljs-comment">//设置背景颜色</span><br>		<span class="hljs-built_in">SetTextColor</span>(hdc, <span class="hljs-built_in">RGB</span>(<span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>)); <span class="hljs-comment">//设置文本颜色</span><br>		<span class="hljs-built_in">SetBkColor</span>(hdc, <span class="hljs-built_in">RGB</span>(<span class="hljs-number">56</span>, <span class="hljs-number">56</span>, <span class="hljs-number">56</span>));   <span class="hljs-comment">//设置背景颜色</span><br>		<span class="hljs-built_in">TextOut</span>(hdc, <span class="hljs-number">95</span>, <span class="hljs-number">90</span>, <span class="hljs-string">L&quot;账户：&quot;</span>, <span class="hljs-number">3</span>);		<span class="hljs-comment">//指定坐标打印文本</span><br>		<span class="hljs-built_in">TextOut</span>(hdc, <span class="hljs-number">95</span>, <span class="hljs-number">130</span>, <span class="hljs-string">L&quot;密码：&quot;</span>, <span class="hljs-number">3</span>);<br>		<span class="hljs-built_in">TextOut</span>(hdc, <span class="hljs-number">80</span>, <span class="hljs-number">170</span>, <span class="hljs-string">L&quot;服务器：&quot;</span>, <span class="hljs-number">4</span>);<br>		HFONT font;  <span class="hljs-comment">//定义一个hfont变量,用于自定义&quot;请登录&quot;三个大字</span><br>		font = <span class="hljs-built_in">CreateFont</span>(<span class="hljs-number">50</span>,<span class="hljs-number">0</span>, <span class="hljs-number">0</span>,<span class="hljs-number">0</span>, FW_NORMAL, FALSE, FALSE, <span class="hljs-number">0</span>, ANSI_CHARSET, OUT_DEFAULT_PRECIS,<br>						CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_QUALITY | FF_SWISS, _T(<span class="hljs-string">&quot;宋体&quot;</span>));<br>        				<span class="hljs-comment">//给font赋值</span><br>		<span class="hljs-built_in">SelectObject</span>(hdc, font);<span class="hljs-comment">//改变将新的font格式传给hdc</span><br>		<span class="hljs-built_in">SetBkMode</span>(hdc, TRANSPARENT);<br>		<span class="hljs-built_in">TextOut</span>(hdc, <span class="hljs-number">100</span>, <span class="hljs-number">20</span>, <span class="hljs-string">L&quot;请 登 录&quot;</span>, <span class="hljs-number">5</span>);<br>		<span class="hljs-built_in">EndPaint</span>(hWnd, &amp;ps);	<span class="hljs-comment">//结束绘图</span><br>		<span class="hljs-keyword">break</span>;<br>	&#125;<br><span class="hljs-comment">//设置登录界面背景颜色</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">OnPaint</span><span class="hljs-params">(HDC hdc)</span></span><br><span class="hljs-function"></span>&#123;<br>	HBRUSH hBrush;<br>	RECT rect;<br>	<span class="hljs-built_in">SetRect</span>(&amp;rect, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-built_in">GetSystemMetrics</span>(SM_CXSCREEN), <span class="hljs-built_in">GetSystemMetrics</span>(SM_CYSCREEN));<br>	hBrush = <span class="hljs-built_in">CreateSolidBrush</span>(<span class="hljs-built_in">RGB</span>(<span class="hljs-number">56</span>, <span class="hljs-number">56</span>, <span class="hljs-number">56</span>));<br>	<span class="hljs-built_in">FillRect</span>(hdc, &amp;rect, hBrush);<br>	hBrush = <span class="hljs-literal">NULL</span>;<br>	<span class="hljs-keyword">return</span>;<br>&#125;<br></code></pre></div></td></tr></table></figure>

<p>EasyX不用那么花里胡哨，只需要只用<code>initgraph(width,height)</code>创建一个绘图窗口，即可开始绘图了。</p>
<p>需要注意的是，如果已经创建了一个绘图窗口了，再次使用该函数绘图时，则会关闭之前的窗口。</p>
<h3 id="图片的处理"><a href="#图片的处理" class="headerlink" title="图片的处理"></a>图片的处理</h3><p>在登录界面未完成的图片处理的遗憾，可以在这样弥补。EasyX对于绘图还是很有一套的，就算你是小白，也能很好的处理这些事情，因为它有中文的<a target="_blank" rel="noopener" href="https://docs.easyx.cn/zh-cn/intro">文档</a></p>
<p>此时图片则是很简单了，直接参照文档就行。先定义一个IMAGE类，然后将图片用<code>loadimage()</code>函数加载到IMAGE类里面，然后再使用<code>putimage()</code>打印图片即可。举个例子</p>
<figure class="highlight c++"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs c++">IMAGE img;<br><span class="hljs-built_in">loadimage</span>(&amp;img, _T(<span class="hljs-string">&quot;IMAGE&quot;</span>), <span class="hljs-built_in">MAKEINTRESOURCE</span>(IDB_PLAYER));<br><span class="hljs-built_in">putimage</span>(<span class="hljs-number">100</span>, <span class="hljs-number">100</span>, &amp;img);<br></code></pre></div></td></tr></table></figure>

<p>当然，<code>loadimage()</code>函数可以不用指定img，如果该参数为NULL，那么就直接将图片打印在屏幕上(以左上角为坐标原点)。它还可以将图片压缩到指定长宽。</p>
<p><code>putimage()</code>函数可以有坐标参数，这意味着你可以在屏幕的任何地方打印任意大小（使用<code>loadimage()</code>）图片。</p>
<h3 id="文字的输出"><a href="#文字的输出" class="headerlink" title="文字的输出"></a>文字的输出</h3><p>对于使用API打印字符的复杂操作而言，EasyX则是太友好了。只需要先定义一个矩形区域，然后在区域内打印即可。至于为什么需要这个矩形，则是为了使背景是透明的，这样才不会影响到背景图片才会更美观。否则就会看到黑色的字体背景填充，简直就是噩梦。</p>
<p>具体的实现方法示例</p>
<figure class="highlight c++"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs c++"><span class="hljs-built_in">settextstyle</span>(<span class="hljs-number">40</span>, <span class="hljs-number">0</span>, <span class="hljs-string">L&quot;方正舒体&quot;</span>);<span class="hljs-comment">//这里设置格式也是简单许多</span><br>RECT r1 = &#123; <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, G_XSIZE, G_YSIZE / <span class="hljs-number">3</span> &#125;;<span class="hljs-comment">//四个参数表示矩形的四个边(左、上、右、下的顺序)的垂直轴坐标,</span><br>									<span class="hljs-comment">//例如左右的边,则是垂直X轴,所以是X坐标,上下则是Y坐标</span><br><span class="hljs-built_in">drawtext</span>(<span class="hljs-string">L&quot;拳王争霸&quot;</span>, &amp;r1, DT_CENTER | DT_VCENTER | DT_SINGLELINE);<span class="hljs-comment">//在矩形区域打印文字</span><br></code></pre></div></td></tr></table></figure>

<p>结合刚刚的图片处理，这个时候一个简单的界面就做好了</p>
<figure class="highlight c++"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs c++"><span class="hljs-comment">//游戏主界面绘制</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">HomeShow</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>&#123;<br>	<span class="hljs-built_in">cleardevice</span>();	<span class="hljs-comment">//清除图形屏幕</span><br>	<span class="hljs-built_in">BeginBatchDraw</span>();<span class="hljs-comment">//开始批量绘图</span><br>	<span class="hljs-built_in">loadimage</span>(<span class="hljs-literal">NULL</span>,_T(<span class="hljs-string">&quot;res/Home.png&quot;</span>),G_XSIZE,G_YSIZE);<br>	<span class="hljs-built_in">setbkmode</span>(TRANSPARENT);<br>	<span class="hljs-built_in">settextstyle</span>(<span class="hljs-number">40</span>, <span class="hljs-number">0</span>, <span class="hljs-string">L&quot;方正舒体&quot;</span>);<br>	RECT r1 = &#123; <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, G_XSIZE, G_YSIZE / <span class="hljs-number">3</span> &#125;;<br>	<span class="hljs-built_in">drawtext</span>(<span class="hljs-string">L&quot;拳王争霸&quot;</span>, &amp;r1, DT_CENTER | DT_VCENTER | DT_SINGLELINE);<br>	<span class="hljs-built_in">settextstyle</span>(<span class="hljs-number">20</span>, <span class="hljs-number">0</span>, <span class="hljs-string">L&quot;宋体&quot;</span>);<br>	RECT r2 = &#123; G_XSIZE / <span class="hljs-number">2</span> - <span class="hljs-number">45</span>,G_YSIZE / <span class="hljs-number">3</span>,G_XSIZE / <span class="hljs-number">2</span> + <span class="hljs-number">45</span>,G_YSIZE / <span class="hljs-number">3</span> + <span class="hljs-number">30</span> &#125;; <span class="hljs-built_in">rectangle</span>(G_XSIZE / <span class="hljs-number">2</span> - <span class="hljs-number">45</span>, G_YSIZE / <span class="hljs-number">3</span>, G_XSIZE / <span class="hljs-number">2</span> + <span class="hljs-number">45</span>, G_YSIZE / <span class="hljs-number">3</span> + <span class="hljs-number">30</span>);<br>	<span class="hljs-built_in">drawtext</span>(<span class="hljs-string">L&quot;创建房间&quot;</span>, &amp;r2, DT_CENTER | DT_VCENTER | DT_SINGLELINE);<br>	RECT r3 = &#123; G_XSIZE / <span class="hljs-number">2</span> - <span class="hljs-number">45</span>,G_YSIZE / <span class="hljs-number">3</span> + <span class="hljs-number">30</span>,G_XSIZE / <span class="hljs-number">2</span> + <span class="hljs-number">45</span>,G_YSIZE / <span class="hljs-number">3</span> + <span class="hljs-number">60</span> &#125;; <span class="hljs-built_in">rectangle</span>(G_XSIZE / <span class="hljs-number">2</span> - <span class="hljs-number">45</span>, G_YSIZE / <span class="hljs-number">3</span> + <span class="hljs-number">30</span>, G_XSIZE / <span class="hljs-number">2</span> + <span class="hljs-number">45</span>, G_YSIZE / <span class="hljs-number">3</span> + <span class="hljs-number">60</span>);<br>	<span class="hljs-built_in">drawtext</span>(<span class="hljs-string">L&quot;加入房间&quot;</span>, &amp;r3, DT_CENTER | DT_VCENTER | DT_SINGLELINE);<br>	RECT r4 = &#123; G_XSIZE / <span class="hljs-number">2</span> - <span class="hljs-number">45</span>,G_YSIZE / <span class="hljs-number">3</span> + <span class="hljs-number">60</span>,G_XSIZE / <span class="hljs-number">2</span> + <span class="hljs-number">45</span>,G_YSIZE / <span class="hljs-number">3</span> + <span class="hljs-number">90</span> &#125;; <span class="hljs-built_in">rectangle</span>(G_XSIZE / <span class="hljs-number">2</span> - <span class="hljs-number">45</span>, G_YSIZE / <span class="hljs-number">3</span> + <span class="hljs-number">60</span>, G_XSIZE / <span class="hljs-number">2</span> + <span class="hljs-number">45</span>, G_YSIZE / <span class="hljs-number">3</span> + <span class="hljs-number">90</span>);<br>	<span class="hljs-built_in">drawtext</span>(<span class="hljs-string">L&quot;关于游戏&quot;</span>, &amp;r4, DT_CENTER | DT_VCENTER | DT_SINGLELINE);<br>	RECT r5 = &#123; G_XSIZE / <span class="hljs-number">2</span> - <span class="hljs-number">45</span>,G_YSIZE / <span class="hljs-number">3</span> + <span class="hljs-number">90</span>,G_XSIZE / <span class="hljs-number">2</span> + <span class="hljs-number">45</span>,G_YSIZE / <span class="hljs-number">3</span> + <span class="hljs-number">120</span> &#125;; <span class="hljs-built_in">rectangle</span>(G_XSIZE / <span class="hljs-number">2</span> - <span class="hljs-number">45</span>, G_YSIZE / <span class="hljs-number">3</span> + <span class="hljs-number">90</span>, G_XSIZE / <span class="hljs-number">2</span> + <span class="hljs-number">45</span>, G_YSIZE / <span class="hljs-number">3</span> + <span class="hljs-number">120</span>);<br>	<span class="hljs-built_in">drawtext</span>(<span class="hljs-string">L&quot;退出游戏&quot;</span>, &amp;r5, DT_CENTER | DT_VCENTER | DT_SINGLELINE);<br>	<span class="hljs-built_in">EndBatchDraw</span>();<span class="hljs-comment">//结束批量绘图</span><br>&#125;<br></code></pre></div></td></tr></table></figure>

<p>这里的G_XSIZE，G_YSIZE是我宏定义的窗口长宽。所以在我登录游戏之后，就得到了一个比登录界面精美的游戏界面</p>
<p><img src="https://gitee.com/Asteri5m/wd_img/raw/master/img/image-20211022152820976.png" srcset="/img/loading.gif" lazyload></p>
<h3 id="鼠标滞留效果的逻辑原理"><a href="#鼠标滞留效果的逻辑原理" class="headerlink" title="鼠标滞留效果的逻辑原理"></a>鼠标滞留效果的逻辑原理</h3><p>一个新的窗口诞生了，但是我们得为这个新的窗口创建消息循环。这里没有键盘的什么事，所以主要看鼠标就好。定义一个鼠标变量，然后获取鼠标消息即可</p>
<figure class="highlight c++"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs c++">MOUSEMSG m;<br>m = <span class="hljs-built_in">GetMouseMsg</span>();<br></code></pre></div></td></tr></table></figure>

<p>在游戏主菜单界面，有一个选择的菜单列表。为了良好的用户使用体验，我在这里增加了“预选”提示：当用户将光标停选项上面时，该选项的背景改变，不再是透明，而是填充为某种颜色，这样比较的提醒，用户的体验就会增加。</p>
<p style="text-align: center;"> 
<div class="group-image-container"><div class="group-image-row"><div class="group-image-wrap"><img id="img1" src= "https://gitee.com/Asteri5m/wd_img/raw/master/img/image-20211022174338052.png"></div><div class="group-image-wrap"><img style = 
          "border-left: 30px solid skyblue;
           border-top: 30px solid transparent;
           border-bottom: 30px solid transparent;"></div><div class="group-image-wrap"><img id="img2" src= "https://gitee.com/Asteri5m/wd_img/raw/master/img/image-20211022174420176.png"></div></div></div>
</p>


<p>看了预览图，现在说说它的逻辑（本来想放动图的……图床不支持）。实现的目标呢：就是根据光标放上去，填充红色。</p>
<p>起初我这样就写了把颜色变红的代码，是这样的：</p>
<figure class="highlight c++"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs c++"><span class="hljs-keyword">while</span> (_HOME)<span class="hljs-comment">//这里的_HOME是判断主菜单界面的关闭与否</span><br>	&#123;<br>		<span class="hljs-built_in">BeginBatchDraw</span>();<span class="hljs-comment">//开始批量绘图,使用批量绘图可以防止闪烁</span><br>		m = <span class="hljs-built_in">GetMouseMsg</span>();<br>		<span class="hljs-built_in"><span class="hljs-keyword">switch</span></span> (m.uMsg)<span class="hljs-comment">//根据鼠标的消息进行相关的操作</span><br>		&#123;<br>		<span class="hljs-keyword">case</span> WM_LBUTTONDOWN:<span class="hljs-comment">//鼠标点击</span><br>				<span class="hljs-keyword">break</span>;<br>		<span class="hljs-keyword">case</span> WM_MOUSEMOVE:<span class="hljs-comment">//鼠标移动</span><br>			RECT r;<br>			<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i&lt;<span class="hljs-number">4</span>; i++)<br>             &#123;<br>             	<span class="hljs-keyword">if</span> (m.x&gt;G_XSIZE / <span class="hljs-number">2</span> - <span class="hljs-number">45</span> &amp;&amp; m.x&lt;G_XSIZE / <span class="hljs-number">2</span> + <span class="hljs-number">45</span> &amp;&amp; <br>                    m.y&gt;G_YSIZE / <span class="hljs-number">3</span> + i * <span class="hljs-number">30</span> &amp;&amp; m.y&lt;G_YSIZE / <span class="hljs-number">3</span> + <span class="hljs-number">30</span> + i * <span class="hljs-number">30</span>)<span class="hljs-comment">//如果光标在区域内</span><br>             	&#123;<br>                    r.left = G_XSIZE / <span class="hljs-number">2</span> - <span class="hljs-number">45</span>;<br>                    r.top = G_YSIZE / <span class="hljs-number">3</span> + i * <span class="hljs-number">30</span>;<br>                    r.right = G_XSIZE / <span class="hljs-number">2</span> + <span class="hljs-number">45</span>;<br>                    r.bottom = G_YSIZE / <span class="hljs-number">3</span> + <span class="hljs-number">30</span> + i * <span class="hljs-number">30</span>;<br>                    POINT points[<span class="hljs-number">8</span>] = &#123; &#123; r.left,r.top &#125;,&#123; r.right,r.top &#125;,<br>                                       &#123; r.right,r.bottom &#125;,&#123; r.left,r.bottom &#125; &#125;;<br>                    <span class="hljs-built_in">setfillcolor</span>(RED);<span class="hljs-comment">//填充颜色</span><br>                    <span class="hljs-built_in">fillpolygon</span>(points, <span class="hljs-number">4</span>);<br>                    <span class="hljs-built_in">setbkmode</span>(TRANSPARENT);<br>                    <span class="hljs-comment">/*打印对应的文字*/</span><br>             	&#125;<br>			&#125;<br>			<span class="hljs-built_in">FlushBatchDraw</span>();<span class="hljs-comment">//结束批量绘图并在窗口上绘图</span><br>			<span class="hljs-keyword">break</span>;<br>		<span class="hljs-keyword">default</span>:<br>			<span class="hljs-keyword">break</span>;<br>		&#125;<br>	&#125;<br></code></pre></div></td></tr></table></figure>

<p>这里用了一个小算法：因为四个框是连续且等高的。他们的X坐标是相同的。<br>此时找到最上面的框的top的Y坐标,我们假设他是 y1.top，同时令框高 h，<br>我们可以算出框底 ：y1.bottom &#x3D; y1.top+h。<br>而框与框之间又是相连的，所以 ：y2.top &#x3D; y1.bottom &#x3D; y1.top<br>依次内推，得到以下公式：<br>$$<br>y_x.top &#x3D; y_1.top+h*(x-1)<br>$$<br>在我的代码里，只是换了一个变量名 i 而已。</p>
<p>但是此时我还没意识到问题的严重性，当我运行测试时才发现，游戏刚打开，是透明的底，没有问题。但是当我把光标移上去再一走的时候，他的背景并没有我想象中的复原。而是一直红在那，甚至最后四个全红。</p>
<p>这事我想了好久，始终没有解决方案，首先，绘图是没有撤销功能的，也就是说我要是改变了那里的颜色，没有办法回去。起初我想到的是把那四个框截图下来，光标移开了再贴上去，就这样覆盖上去就完事了。但是这种办法显然不可取：</p>
<ul>
<li>其一：若是我换了背景图片又得从头再截图</li>
<li>其二：在加入房间时，有一个房间列表供玩家选择，但是这玩意是动态的，没办法截图</li>
</ul>
<p>因此我否定了这个方案，最终我找到了解决方案：我在上面的for循环的if语句后面添了几行代码</p>
<figure class="highlight c++"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs c++"><span class="hljs-keyword">else</span><br>&#123;<br>    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">getpixel</span>(G_XSIZE / <span class="hljs-number">2</span> - <span class="hljs-number">45</span> + <span class="hljs-number">1</span>, G_YSIZE / <span class="hljs-number">3</span> + i * <span class="hljs-number">30</span> + <span class="hljs-number">1</span>) == RED)<br>    &#123;<br>        <span class="hljs-built_in">HomeShow</span>();<span class="hljs-comment">//重载</span><br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>

<p>就这几行代码，完美的解决了我的问题。首先，我把显示界面的所有绘图操作封装成函数，然后在鼠标移动的过程中，判断是否在选项上方，如果在，则填充红色，重点在于：当鼠标移开了之后，我就判断它是不是填充了红色，如果是，则重新加载这个界面。如此就可以“复原”的效果了。但它的本质是“刷新”。</p>
<pre><code class=" mermaid">graph TB
a(登录成功)--&gt;b[主菜单界面]
b--&gt;c[鼠标移动]
b--&gt;c1[鼠标点击]
c-.未在选项上.-&gt;d[无变化]
c--在选项上--&gt;e[填充红色] 
e--光标还在--&gt;f[保持红色]
e-.光标移开.-&gt;g[&quot;刷新&quot;]
d--&gt;h[消息循环结束]
f--&gt;h
g--&gt;h
h==新的循环==&gt;b
</code></pre>

<h3 id="虚拟按钮的实现及响应事件"><a href="#虚拟按钮的实现及响应事件" class="headerlink" title="虚拟按钮的实现及响应事件"></a>虚拟按钮的实现及响应事件</h3><p>与win32的实体按钮不同，EasyX是不支持这一类的，这一点从它的的图形库中可以看出来，但这并不是意味着我们没有办法实现它，实现的原理也很简单。</p>
<p>在上面的 <code>GetMouseMsg()</code> 函数中，我们得到了不只是鼠标的位置信息，还有各种鼠标消息，例如是否点击，是左键还是右键等等。</p>
<p>这里我们只需要用到左键点击即可，所以在得到 <code>WM_LBUTTONDOWN</code> 的消息时，进行相应的操作即可。例如这样：</p>
<figure class="highlight c++"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs c++"><span class="hljs-keyword">case</span> WM_LBUTTONDOWN:<br>    <span class="hljs-built_in">EndBatchDraw</span>();<span class="hljs-comment">//结束批量绘图</span><br>    <span class="hljs-keyword">if</span> (m.x&gt;G_XSIZE / <span class="hljs-number">2</span> - <span class="hljs-number">45</span> &amp;&amp; m.x&lt;G_XSIZE / <span class="hljs-number">2</span> + <span class="hljs-number">45</span> &amp;&amp; m.y&gt;G_YSIZE / <span class="hljs-number">3</span> &amp;&amp; m.y&lt;G_YSIZE / <span class="hljs-number">3</span> + <span class="hljs-number">30</span> &amp;&amp; _HOME == <span class="hljs-number">1</span> &amp;&amp; _ABOUTGAME == <span class="hljs-number">0</span>)<br>        <span class="hljs-comment">//如果选择“创建房间”</span><br>    &#123;<br>        _HOME = <span class="hljs-literal">false</span>;<br>        <span class="hljs-keyword">if</span> (<span class="hljs-built_in">playGame</span>(<span class="hljs-number">1</span>))<span class="hljs-comment">//创建房间请求，失败返回非零，成功则进入房间，直到退出房间</span><br>            <span class="hljs-built_in">MessageBox</span>(<span class="hljs-literal">NULL</span>, <span class="hljs-string">L&quot;房间数已达上限&quot;</span>, <span class="hljs-string">L&quot;提示&quot;</span>, <span class="hljs-literal">NULL</span>);<br>        _HOME = <span class="hljs-literal">true</span>;<br>        <span class="hljs-built_in">HomeShow</span>();<br>        <span class="hljs-keyword">break</span>;<br>    &#125;<br>    <span class="hljs-comment">/*else if(在其他位置)</span><br><span class="hljs-comment">    &#123;</span><br><span class="hljs-comment">        对应操作;</span><br><span class="hljs-comment">    &#125;*/</span><br>    <span class="hljs-keyword">else</span><br>        <span class="hljs-keyword">break</span>;<br></code></pre></div></td></tr></table></figure>

<p>在得到鼠标点击的消息后，即可判断鼠标的位置是否在我们设置的“虚拟按钮”的位置内，如果在则响应事件，由此，即可完成一次点击事件的响应。</p>
<h3 id="多线程绘图"><a href="#多线程绘图" class="headerlink" title="多线程绘图"></a>多线程绘图</h3><p>为什么会用到多线程绘图?</p>
<p>当你使用单线程完成不了你的需求的时候，就需要使用多线程来完成。我现在面临的情况也一样，在我的绘图窗口，它有可以点击的按钮，还有鼠标移动到特定位置时相关的绘图的要求，以及一张自始至终都在循环的动图。</p>
<p>此时需要解决的问题：</p>
<ol>
<li>EasyX是不支持直接放置GIF的，可以加载，但只会显示第一帧</li>
<li>鼠标和动图需要分开处理，鼠标的消息不影响动图的循环</li>
</ol>
<p>第一个问题其实很好解决，动图嘛，无外乎几张图片切换，就直接把放置图片的那段代码循环然后每次加载不同的图片即可。起初我是直接放在的消息循环里面的，和消息是同步的嘛，单线程嘛，在当前函数就不能处理其他函数了（在函数内调用其他函数说白了也在这个大循环里面，也是外函数的一部分了）。代码运行之后，动图就变得及其“诡异”。鼠标移动则动图循环，鼠标无动作则动图停。</p>
<p>折腾了半天才找到原因： <code>GetMouseMsg()</code> 函数是一个阻塞函数，这就意味着，如果没有消息，程序就会停下来等待消息，所以才造成了刚刚那一幕。所以游戏主页面的实现需要多线程了，随即我就把展示动图那一步拿出来，封装成函数，在游戏进入主界面的时候在一个新的线程启动即可</p>
<figure class="highlight c++"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs c++"><span class="hljs-comment">//主游戏界面</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">CreateRoom</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>&#123;<br>	HWND G_hWnd = <span class="hljs-built_in">initgraph</span>(G_XSIZE, G_YSIZE);<span class="hljs-comment">//创建游戏窗口，同时自动销毁之前的绘图窗口</span><br>	<span class="hljs-built_in">MoveWindow</span>(G_hWnd, <span class="hljs-number">500</span>, <span class="hljs-number">230</span>, G_XSIZE + <span class="hljs-number">16</span>, G_YSIZE + <span class="hljs-number">39</span>, FALSE);<span class="hljs-comment">//移动窗口到指定位置</span><br>	_GamePlayS();<span class="hljs-comment">//绘图操作</span><br><br>	flag_T = <span class="hljs-number">1</span>;<span class="hljs-comment">//线程结束标识</span><br>	hThread = <span class="hljs-built_in">CreateThread</span>(<span class="hljs-literal">NULL</span>, <span class="hljs-number">0</span>, (LPTHREAD_START_ROUTINE)my_GIF, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>);<span class="hljs-comment">//启动线程</span><br><br>	<span class="hljs-keyword">int</span> flag_i = <span class="hljs-number">0</span>;<span class="hljs-comment">//己方动图flag</span><br>	MOUSEMSG m;<br>	<span class="hljs-comment">//消息循环</span><br>	<span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>)<br>	&#123;<br>        <span class="hljs-comment">/*游戏游玩过程的响应函数等等*/</span><br>    &#125;<br>&#125;<br><span class="hljs-comment">//动图</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">my_GIF</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>&#123;<br>	<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>;<br>	<span class="hljs-keyword">while</span> (flag_T)<br>	&#123;<br>		<span class="hljs-comment">//循环绘图</span><br>	&#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>

<p>创建线程之后，也需要关闭线程，所以在检测到玩家点击返回时就会有一下操作</p>
<figure class="highlight c++"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs c++"><span class="hljs-keyword">if</span> (m.x &gt; G_XSIZE - <span class="hljs-number">46</span> &amp;&amp; m.x&lt;G_XSIZE - <span class="hljs-number">3</span> &amp;&amp; m.y&gt;G_YSIZE - <span class="hljs-number">26</span>)<span class="hljs-comment">//点击返回</span><br>&#123;<br>    <span class="hljs-keyword">char</span> Buffer_My[] = &#123; <span class="hljs-number">101</span>,<span class="hljs-number">0</span> &#125;;<br>    _SendMessage(Sock_Play, Buffer_My);<span class="hljs-comment">//发送“我已退出”消息到服务器</span><br>    flag_T = <span class="hljs-number">0</span>;<span class="hljs-comment">//关闭线程flag</span><br>    flag_j = <span class="hljs-number">0</span>;<span class="hljs-comment">//清空我的选择</span><br>    <span class="hljs-built_in">Sleep</span>(<span class="hljs-number">100</span>);<br>    <span class="hljs-built_in">CloseHandle</span>(hThread);<span class="hljs-comment">//关闭线程</span><br>    <span class="hljs-keyword">return</span>;<br>&#125;<br></code></pre></div></td></tr></table></figure>

<p>这是在 <code>case WM_LBUTTONDOWN</code>下的操作，可以看到，我退出后还会发送消息给服务器，服务器再将我退出的消息发送给另一位玩家，然后本局游戏完整结束。</p>
<p><strong>注意：</strong>开启多线程，特别是该线程里的函数还是死循环的时候，就应该将循环改为可以跳出或者结束的非死循环，既添加循环结束标识，这个标识同样可以作为线程结束标识，此时，线程才能使用 <code>CloseHandle()</code> 关闭。必须关闭，否则可能会一直在你的进程中，最后结束进程了可能该线程还会保留，占用内存资源</p>
<hr>
<section class="footnotes"><div class="footnote-list"><ol><li><span id="fn:1" class="footnote-text"><span>这里其实是依据窗口风格指明一个子窗口标识。对于层叠或弹出式窗口，hMenu指定窗口使用的菜单：如果使用了菜单类，则hMenu可以为NULL。对于子窗口，hMenu指定了该子窗口标识（一个<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E6%95%B4%E5%9E%8B%E9%87%8F">整型量</a>），一个对话框使用这个整型值将事件通知父类。应用程序确定子窗口标识，这个值对于相同父窗口的所有子窗口必须是唯一的。
<a href="#fnref:1" rev="footnote" class="footnote-backref"> ↩</a></span></span></li><li><span id="fn:2" class="footnote-text"><span>懂得都懂
<a href="#fnref:2" rev="footnote" class="footnote-backref"> ↩</a></span></span></li></ol></div></section>
            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E9%9A%8F%E6%89%8B%E8%AE%B0%C2%B7%E4%B8%80%E4%BA%9B%E5%AE%89%E5%85%A8%E4%B9%8B%E5%A4%96%E7%9A%84%E6%8A%80%E6%9C%AF/">随手记·一些安全之外的技术</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Win32/">Win32</a>
                    
                      <a class="hover-with-bg" href="/tags/EasyX/">EasyX</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/archives/Fluid%E9%AD%94%E6%94%B9%E7%AC%94%E8%AE%B0.html">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Fluid魔改笔记</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/archives/PE%E6%96%87%E4%BB%B6%E7%BB%93%E6%9E%84%E7%AE%80%E6%9E%90.html">
                        <span class="hidden-mobile">PE文件结构简析</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments" lazyload>
                
                  
                
                
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://cdn.jsdelivr.net/gh/HCLonely/Valine@latest/dist/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"5INqyf5xMrWdsn0whn39qjsu-gzGzoHsz","appKey":"6UTAOxyJnjvDwHX3PJagKMg9","path":"window.location.pathname","placeholder":"输入QQ号我就能获取你的企鹅昵称和头像啦~","avatar":"retro","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":false,"recordIP":false,"serverURLs":"","emojiCDN":null,"emojiMaps":null,"enableQQ":true,"tagMeta":["博主","小伙伴","访客"],"master":"3bbfd45c0631973d5196327805f62511","friends":["2012b2da9e0852350b42a9c21823f3cf","641f794b535aea2ebe2ad543ec35e2f8"]},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          Fluid.plugins.initFancyBox('#valine .vcontent img:not(.vemoji)');
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
      <div class="col-lg-7 mx-auto nopadding-x-md">
        <div class="container custom mx-auto">
          <meting-js server="netease" type="playlist" id="5413938648" fixed="true" theme="#aa55ff"></meting-js>
        </div>
      </div>
    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>使用Hexo框架</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>精品Fluid主题</span></a><br> <span id="timeDate">天数载入中</span><span id="times">...</span><br> 
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- LeanCloud 统计PV -->
        <span id="leancloud-site-pv-container" style="display: none">
            总访问量 
            <span id="leancloud-site-pv"></span>
             次
          </span>
      
      
        <!-- LeanCloud 统计UV -->
        <span id="leancloud-site-uv-container" style="display: none">
            总访客数 
            <span id="leancloud-site-uv"></span>
             人
          </span>
      

    
  </div>


  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        蜀ICP备2021029058号
      </a>
    </span>
    
      
        <span>
          <a
            href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=51011202000479"
            rel="nofollow noopener"
            class="beian-police"
            target="_blank"
          >
            
              <span style="visibility: hidden; width: 0">|</span>
              <img src="/img/beian.png" srcset="/img/loading.gif" lazyload alt="police-icon"/>
            
            <span>川公网安备 51011202000479号</span>
          </a>
        </span>
      
    
  </div>


  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  




  <script defer src="/js/leancloud.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>





  

  
    <!-- MathJax -->
    <script>
      MathJax = {
        tex: {
          inlineMath: [['$', '$'], ['\\(', '\\)']]
        },
        loader: {
          load: ['ui/lazy']
        },
        options: {
          renderActions: {
            findScript: [10, doc => {
              document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
                const display = !!node.type.match(/; *mode=display/);
                const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
                const text = document.createTextNode('');
                node.parentNode.replaceChild(text, node);
                math.start = { node: text, delim: '', n: 0 };
                math.end = { node: text, delim: '', n: 0 };
                doc.math.push(math);
              });
            }, '', false],
            insertedScript: [200, () => {
              document.querySelectorAll('mjx-container').forEach(node => {
                let target = node.parentNode;
                if (target.nodeName.toLowerCase() === 'li') {
                  target.parentNode.classList.add('has-jax');
                }
              });
            }, '', false]
          }
        }
      };
    </script>

    <script async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js" ></script>

  





  <script  src="https://cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js" ></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({"theme":"default"});
    }
  </script>




  

  

  

  

  

  




  
<script src="//cdn.jsdelivr.net/npm/aplayer@1.10.0/dist/APlayer.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/meting@2.0.1/dist/Meting.min.js"></script>
<script src="/xm_custom/custom.js"></script>



<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
